package com.future.mayi.adapter;

import android.content.Context;
import android.support.annotation.AnimRes;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;


import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 周大虎 on 2017/9/21.
 */

public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected XRecyclerView xRecyclerView;
    protected int mLastPosition = -1;
    protected List<T> mList;
    protected OnItemClickListener mOnItemClickListener;
    protected Context mContext;

    public BaseRecyclerViewAdapter(Context context, List<T> list) {
        mContext = context;
        mList = list;
    }

    public BaseRecyclerViewAdapter(Context context, List<T> list, XRecyclerView xRecyclerView) {
        mContext = context;
        mList = list;
        this.xRecyclerView = xRecyclerView;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }


    protected View getView(ViewGroup parent, int layoutId) {
        return LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
    }

    @Override
    public int getItemCount() {
        if (mList == null) {
            return 0;
        }
        return mList.size();
    }

    protected void setItemAppearAnimation(RecyclerView.ViewHolder holder, int position, @AnimRes int type) {
        if (position > mLastPosition/* && !isFooterPosition(position)*/) {
            Animation animation = AnimationUtils.loadAnimation(holder.itemView.getContext(), type);
            holder.itemView.startAnimation(animation);
            mLastPosition = position;
        }
    }


    public void add(int position, T item) {
        mList.add(position, item);
        notifyItemInserted(position);
    }

    public void addMore(List<T> data) {
        if (mList == null) mList = new ArrayList<>();
        mList.addAll(data);
        notifyDataSetChanged();
    }

    public void addMore(T data) {
        if (mList == null) mList = new ArrayList<>();
        mList.add(data);
        notifyDataSetChanged();
    }

    public void delete(int position) {
        if (xRecyclerView != null) {

            xRecyclerView.notifyItemRemoved(mList, position);
            return;
        }
        if ((getItemCount() - 1) < position) return;
        mList.remove(position);
        notifyDataSetChanged();
    }

    public List<T> getList() {
        return mList;
    }

    public void setList(List<T> items) {
        mList = items;
        notifyDataSetChanged();
    }

    public void clear() {
        if (mList == null || mList.size() == 0) return;
        mList.clear();
        notifyDataSetChanged();
    }


    public T getItem(int position) {
        if (position > -1 && mList != null && mList.size() > position) return mList.get(position);
        else return null;
    }


    protected void setText(TextView textView, CharSequence content) {
        if (textView == null)
            return;
        textView.setText(TextUtils.isEmpty(content) ? "" : content);
    }

    protected void setText(TextView textView, int resourceId) {
        if (textView == null)
            return;
        textView.setText(mContext.getResources().getString(resourceId));
    }

    protected void loadImg(ImageView imageView, String imgUrl) {
        HCUtils.loadWebImg(mContext,imageView,imgUrl);
    }

    protected void loadImg(ImageView imageView, int resId) {
        HCUtils.loadWebImg(mContext, imageView, resId);
    }
}
